package gr.atc.epart;

import gr.atc.epart.googlewrapper.moderator.ModeratorSubmission;
import gr.atc.epart.googlewrapper.moderator.ModeratorTopic;

import java.util.ArrayList;

public class SurveyQuestion
{
	public enum QuestionType
	{
		YesNo,
		MultipleChoice,
		MultipleSelect
	}

	private QuestionType _type;
	
	public SurveyQuestion(QuestionType type)
	{
		_type = type;	
	}
	
	public void setType(QuestionType type)
	{
		_type = type;
	}
	
	public QuestionType getType()
	{
		return _type;
	}

	public static ArrayList<SurveyQuestion> questionsFromTopics(ArrayList<ModeratorTopic> topics)
	{
		// in order to support the survey through Google moderator
		// there is the following convention
		// 3 types of Questions are supported
		// a) YES / NO type
		// b) Multiple-choice type (only one selection from many options)
		// c) Multiple-select type (one or more selections from many options)
		//
		// each YES/NO question is submitted to GM as a topic 
		// the title of the topic is a unique incremental number precedded by "%"
		// the description is the question text and then a submission by the same name is created
		//
		// each Multiple choice question is submitted to GM as a topic 
		// the title of the topic is a unique incremental number precedded by "#"
		// the description is the question text and then for each option 
		// a submission by the same name is created
		//
		// each Multiple select question is submitted to GM as a topic 
		// the title of the topic is a unique incremental number precedded by "$"
		// the description is the question text and then for each option 
		// a submission by the same name is created
		//
		
		ArrayList<SurveyQuestion> surveyQuestionList = new ArrayList<SurveyQuestion>();
		
		try
		{
			for (ModeratorTopic topic:topics)
			{
				if (topic.getName().startsWith("%"))
				{
					// this is a simple Yes/No type of question
					// we create one topic with one submission

					//this check should be here in case someone creates the questions from the web interface and 
					// does not follow the rule...

					if (topic.getSubmissionList().size()>0)
					{
						ModeratorSubmission submission = topic.getSubmissionList().get(0); 


						int yesVotes = submission.getCounter(ModeratorSubmission.PLUS_VOTES);
						int noVotes = submission.getCounter(ModeratorSubmission.MINUS_VOTES);

						SurveyLinkToModerator link = new SurveyLinkToModerator(submission.getOnwerSeriesId(),submission.getOnwerTopic(),submission.getId());
						SurveyYesNoQuestion question = new SurveyYesNoQuestion(submission.getText(),link);
						question.setYesVotesNumber(yesVotes);
						question.setNoVotesNumber(noVotes);
						
						surveyQuestionList.add(question);
					}
				}
				else if (topic.getName().startsWith("#"))
				{
					// this is a multiple choice question
					SurveyMultipleChoiceQuestion question = new SurveyMultipleChoiceQuestion(topic.getDescription());

					for (ModeratorSubmission submission:topic.getSubmissionList())
					{
						SurveyLinkToModerator link = new SurveyLinkToModerator(submission.getOnwerSeriesId(),submission.getOnwerTopic(),submission.getId());
						question.addOption(submission.getText(),link);
					}

					surveyQuestionList.add(question);
				}
				else if (topic.getName().startsWith("$"))
				{
					// this is a multiple selection question
					SurveyMultipleSelectionQuestion question = new SurveyMultipleSelectionQuestion(topic.getDescription());

					for (ModeratorSubmission submission:topic.getSubmissionList())
					{
						SurveyLinkToModerator link = new SurveyLinkToModerator(submission.getOnwerSeriesId(),submission.getOnwerTopic(),submission.getId());
						question.addOption(submission.getText(),link);
					}

					surveyQuestionList.add(question);
				}
			}
		}catch(Exception e)
		{

		}
		
		return surveyQuestionList;
	}
	
	
	
}
